package com.ruoyi.project.utils.Serial;

import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ChuanKouListener implements SerialPortEventListener {

    // 串口输入流引用
    InputStream inputStream;
    // 串口输出流引用
    OutputStream outputStream;

    public ChuanKouListener(InputStream inputStream, OutputStream outputStream) {
        this.inputStream = inputStream;
        this.outputStream = outputStream;
    }

    // 堵塞队列：用来存放串口发送到服务端的数据
    public static BlockingQueue<String> msgQueue = new LinkedBlockingQueue();

    @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
        switch (serialPortEvent.getEventType()) {
            /*
             *  SerialPortEvent.BI:/*Break interrupt,通讯中断
             *  SerialPortEvent.OE:/*Overrun error，溢位错误
             *  SerialPortEvent.FE:/*Framing error，传帧错误
             *  SerialPortEvent.PE:/*Parity error，校验错误
             *  SerialPortEvent.CD:/*Carrier detect，载波检测
             *  SerialPortEvent.CTS:/*Clear to send，清除发送
             *  SerialPortEvent.DSR:/*Data set ready，数据设备就绪
             *  SerialPortEvent.RI:/*Ring indicator，响铃指示
             *  SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty，输出缓冲区清空
             */
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            // 当有可用数据时读取数据
            case SerialPortEvent.DATA_AVAILABLE:
                // 数据接收缓冲容器
                byte[] readBuffer = new byte[1024];
                try {
                    readBuffer = new byte[inputStream.available()];
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    // 存储待接收读取字节数大小
                    int numBytes = 0;
                    while (inputStream.available() > 0) {
                        numBytes = inputStream.read(readBuffer);
                        if (numBytes > 0) {
                            // 转换为16进制字符串
                            StringBuilder hexBuilder = new StringBuilder();
                            for (byte b : readBuffer) {
                                hexBuilder.append(String.format("%02X ", b));
                            }
                            msgQueue.add(new String(hexBuilder));
                            // 数据接收缓冲容器清空初始化
                            readBuffer = new byte[0];
                        }
                    }
                } catch (IOException e) {
                    System.out.println("00异常00");
                }
                break;
        }
    }
}

